#!/usr/bin/env bash

#####################################################################################
# THIS MODULE ALLOWS YOU TO DOWNLOAD THE INSTALLATION SCRIPTS WITHOUT INSTALLING THEM
#####################################################################################

function _convert_to_appman_compatible_script() {
	if grep -q -- "--convert" "$AMCACHEDIR"/download-args; then
		if test -f "$APPMANCONFIG"/appman-config; then
			APPSPATH=$(cat "$APPMANCONFIG"/appman-config)
			sed -i "s# /usr/local/bin# $HOME/.local/bin#g" ./$arg
			sed -i "s# /usr/games# $HOME/.local/bin#g" ./$arg
			sed -i "s# /usr/local/games# $HOME/.local/bin#g" ./$arg
			sed -i "s# /usr/local/share/applications# $DATADIR/applications#g" ./$arg
			sed -i "s# /usr/local/share/pixmaps# $DATADIR/icons#g" ./$arg
			sed -i "s# /opt# $HOME/$APPSPATH#g" ./$arg
			sed -i "s#/opt/#$HOME/$APPSPATH/#g" ./$arg
			sed -i "s# https://api.github.com#$HeaderAuthWithGITPAT https://api.github.com#g" ./$arg
			echo 'sed -i "s#Icon=/opt#Icon=$HOME/$APPSDIR#g" $DATADIR/applications/AM*.desktop' >> ./$arg
			echo 'sed -i "s#Exec=$APP#Exec=$HOME/.local/bin/$APP#g" $DATADIR/applications/AM*.desktop' >> ./$arg
			echo 'sed -i "s#Exec=/usr/bin/#Exec=$HOME/.local/bin/#g" $DATADIR/applications/AM*.desktop' >> ./$arg
			echo 'sed -i "s#Exec=/opt/#Exec=$HOME/$APPSDIR/#g" $DATADIR/applications/AM*.desktop' >> ./$arg
			echo -ne " Converting $arg to an AppMan-compatible script.\r" && sleep 1 &&
			echo -ne "                                                       \r"
		else
			echo ' 💀 ERROR: "--convert" requires a configuration file in ~/.config/appman'
		fi
	fi
}

function _download() {
	case $arg in
	'--convert')	;;
	*) 
		if test -f "$AMPATH/neodb"; then
			rm -R -f "$AMCACHEDIR/multirepo-args"
			MULTIREPO=$(cat "$AMPATH/neodb" | grep "Source=" | sed 's/Source=//g')
			for anyrepo in $MULTIREPO; do
				if curl --output /dev/null --silent --head --fail $anyrepo/$arg  1>/dev/null; then
					echo "$anyrepo" >> "$AMCACHEDIR/multirepo-args"
				fi
			done
			if test -f "$AMCACHEDIR/multirepo-args"; then
				anyrepoargs=$(cat "$AMCACHEDIR/multirepo-args" 2>/dev/null | wc -l)
				if [ $anyrepoargs -gt 0 ]; then
					if curl --output /dev/null --silent --head --fail $APPSDB/$arg  1>/dev/null; then
						echo "$APPSDB" >> "$AMCACHEDIR/multirepo-args"
					fi
					anyrepoargall=$(cat "$AMCACHEDIR/multirepo-args" 2>/dev/null | wc -l)
					if [ $anyrepoargall == 1 ]; then
						cd "$SCRIPTDIR" || return; wget -q "$(cat "$AMCACHEDIR/multirepo-args" | head -1)/$arg";  _convert_to_appman_compatible_script
						rm -R -f "$AMCACHEDIR/multirepo-args"
						echo ' ◆ "'$arg'" third-party installation script downloaded! '
					else
						echo -e " ◆ FOUND $(echo '"'$arg'"' | tr a-z A-Z) FROM MULTIPLE SOURCES:\n" &&
						printf " Select a URL from this menu (read carefully) or press CTRL+C to abort:\n\n"; sleep 1;
						select d in $(cat "$AMCACHEDIR/multirepo-args"); do test -n "$d" && break; echo ">>> Invalid Selection"; done
						rm -R -f "$AMCACHEDIR/multirepo-args"
						cd "$SCRIPTDIR" || return; wget -q $d/$arg;  _convert_to_appman_compatible_script
						echo ' ◆ "'$arg'" installation script downloaded! '
					fi
				elif curl --output /dev/null --silent --head --fail $APPSDB/$arg  1>/dev/null; then
					rm -R -f "$AMCACHEDIR/multirepo-args"
					cd "$SCRIPTDIR" || return; wget -q $APPSDB/$arg;  _convert_to_appman_compatible_script
					echo ' ◆ "'$arg'" installation script downloaded! '
				else
					echo ' 💀 ERROR: "'$arg'" does NOT exist in the database, see "'$AMCLI' -l"'
				fi
			elif curl --output /dev/null --silent --head --fail $APPSDB/$arg  1>/dev/null; then
				cd "$SCRIPTDIR" || return; wget -q $APPSDB/$arg;  _convert_to_appman_compatible_script
				echo ' ◆ "'$arg'" installation script downloaded! '
			else
				echo ' 💀 ERROR: "'$arg'" does NOT exist in the database, see "'$AMCLI' -l"'
			fi
		elif curl --output /dev/null --silent --head --fail $APPSDB/$arg  1>/dev/null; then
			cd "$SCRIPTDIR" || return; wget -q $APPSDB/$arg;  _convert_to_appman_compatible_script
			echo ' ◆ "'$arg'" installation script downloaded! '
		elif curl --output /dev/null --silent --head --fail $AMREPO/testing/$arch/$arg  1>/dev/null; then
			echo ' ⚠️ "'$arg'" downloaded from "testing", the unstable branch '; echo ""; echo ' WARNING! PROGRAMS COMING FROM "TESTING" ARE BROKEN, USE AT YOUR OWN RISK!'
			cd "$SCRIPTDIR" || return; wget -q $AMREPO/testing/$arch/$arg; _convert_to_appman_compatible_script
		else
			echo ' 💀 ERROR: "'$arg'" is NOT a valid argument'
		fi
	esac
}

case $2 in
'')	echo " USAGE: $AMCLI $1 [ARGUMENT]"
	echo "        $AMCLI $1 --convert [ARGUMENT]"; exit;;
esac

while [ -n "$1" ]; do
	rm -f "$AMCACHEDIR"/download-args
	echo "$@" | tr ' ' '\n' >> "$AMCACHEDIR"/download-args && echo STOP >> "$AMCACHEDIR"/download-args
	ARGS=$(tail -n +2 "$AMCACHEDIR"/download-args)

	for arg in $ARGS; do
		if [ $arg == STOP ]; then
			exit
		else
			_download
		fi

	done
done

shift
